package com.wing.order.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.enterprise.feign.EnterpriseClient;
import com.wing.enterprise.product.model.response.ProductDetailVO;
import com.wing.enterprise.product.model.response.ProductUnitVO;
import com.wing.enterprise.product.model.response.ProductVO;
import com.wing.order.model.entity.InventoryOutPlan;
import com.wing.order.model.entity.InventoryOutPlanDetail;
import com.wing.order.model.request.InventoryOutPlanForm;
import com.wing.order.model.response.InventoryOutPlanDetailVO;
import com.wing.order.model.response.InventoryOutPlanVO;
import com.wing.order.service.InventoryOutPlanDetailService;
import com.wing.order.service.InventoryOutPlanService;
import com.wing.platform.feign.PlatformClient;
import com.wing.warehouse.model.entity.Warehouse;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import com.wing.web.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author
 */
@Validated
@Api(value = "Admin-出库单计划管理", tags = "#/order/inventoryOutPlan")
@RestController("inventoryOutPlanController")
@RequestMapping("order/admin/inventoryOutPlan")
@RequiredArgsConstructor
public class InventoryOutPlanController {

    private final InventoryOutPlanService inventoryOutPlanService;
    private final InventoryOutPlanDetailService inventoryOutPlanDetailService;
    private final EnterpriseClient enterpriseClient;
    private final PlatformClient platformClient;

    @ApiOperation(value = "查询所有出库单计划", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<InventoryOutPlan> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,InventoryOutPlan inventoryOutPlan
    ){

        //搜索
        IPage<InventoryOutPlan> list = inventoryOutPlanService.getInventoryOutPlanPageList(inventoryOutPlan,page,limit);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }



    @ApiOperation(value = "添加出库单计划", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@Validated(InventoryOutPlanForm.Add.class) InventoryOutPlanForm inventoryOutPlanForm) {
        return inventoryOutPlanService.add(inventoryOutPlanForm);
    }

    @ApiOperation(value = "添加交付计划出库单计划", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/deliveryOutPlanAdd")
    public JsonResult deliveryOutPlanAdd(@Validated(InventoryOutPlanForm.Add.class)@RequestBody InventoryOutPlanForm inventoryOutPlanForm) {
        return inventoryOutPlanService.add(inventoryOutPlanForm);
    }




    @ApiOperation(value = "修改出库单计划", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@Validated(InventoryOutPlanForm.Update.class) InventoryOutPlanForm inventoryOutPlanForm) {
        InventoryOutPlan inventoryOutPlan = inventoryOutPlanService.getById(inventoryOutPlanForm.getId());
        if(inventoryOutPlan == null){
        return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(InventoryOutPlan.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(inventoryOutPlanForm, inventoryOutPlan, copyOptions);
        if (inventoryOutPlanService.updateById(inventoryOutPlan)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }



@ApiOperation(value = "删除出库单计划", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inventoryOutPlanId", value = "出库单计划id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/{id}" )
    public JsonResult delete(@PathVariable("id") Long inventoryOutPlanId) {
        if (inventoryOutPlanService.removeById(inventoryOutPlanId)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }
    @GetMapping("/getProductDetailList")
    public JsonResult getProductDetailList(@RequestParam(value = "productSkuCodeArr[]") String[] productSkuCodeArr){
        List<ProductDetailVO> list = new ArrayList<ProductDetailVO>();
        for (String item : productSkuCodeArr){
            JsonResult<ProductDetailVO> productDetailJson = enterpriseClient.getProductDetailBySkuCode(item);
            JsonResult<ProductVO> productJson = enterpriseClient.getProductById(productDetailJson.getData().getProductId());
            productDetailJson.getData().setProductName(productJson.getData().getName());
            productDetailJson.getData().setProductPic(productJson.getData().getPicture());
            JsonResult<ProductUnitVO> unitJson = enterpriseClient.getProductUnitById(productJson.getData().getUnitId());
            productDetailJson.getData().setUnitId(unitJson.getData().getId());
            productDetailJson.getData().setUnitName(unitJson.getData().getName());
            list.add(productDetailJson.getData());
        }
        return JsonResult.success(list);
    }
    @GetMapping("/getInventoryOutPlanById")
    public JsonResult getInventoryOutPlanById(@RequestParam(value = "id") Long id){
        InventoryOutPlanVO info = inventoryOutPlanService.getInventoryOutPlanById(id);
        return JsonResult.success(info);
    }
    @ApiOperation(value = "查询出库计划商品信息", notes = "查询")
    @GetMapping("/getInventoryOutPlanDetailVOList")
    public JsonResult getInventoryOutPlanDetailVOList(@RequestParam(value = "planId") Long planId) {
        List<InventoryOutPlanDetailVO> list = inventoryOutPlanDetailService.getInventoryOutPlanDetailVOList(planId);
        return JsonResult.success(list);
    }
    @ApiOperation(value = "审核出库单计划", notes = "修改")
    @PutMapping("/updateStatus")
    public JsonResult updateStatus(@RequestParam(value = "id") Long id) {
        InventoryOutPlan inventoryOutPlan = inventoryOutPlanService.getById(id);
        if(inventoryOutPlan == null){
            return JsonResult.fail("修改失败！");
        }
        inventoryOutPlan.setStatus(InventoryOutPlan.Status.confirm);
        if (inventoryOutPlanService.updateById(inventoryOutPlan)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }
    @ApiOperation(value = "修改出库商品的数量", notes = "修改")
    @PutMapping("/updateOutQuantity")
    public JsonResult updateOutQuantity(@RequestParam(value = "productJson") String productJson){
        JSONArray jsonArray = JSONArray.parseArray(productJson);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject obj = (JSONObject) jsonArray.get(i);
            Long id = Long.parseLong(obj.getString("id"));
            InventoryOutPlanDetail inventoryOutPlanDetail = inventoryOutPlanDetailService.getById(id);
            if(inventoryOutPlanDetail == null){
                return JsonResult.fail("修改失败！");
            }
            Long quantity =  Long.parseLong(obj.getString("quantity"));
            inventoryOutPlanDetail.setQuantity(quantity);
            inventoryOutPlanDetailService.updateById(inventoryOutPlanDetail);

        }
        return JsonResult.success("修改成功！");
    }
    @ApiOperation(value = "查询所有出库计划", notes = "查询")
    @GetMapping("/getPlanAll")
    public JsonResult getPlanAll(){
        List<InventoryOutPlan> list = inventoryOutPlanService.getPlanAll();
        return JsonResult.success(list);
    }
    @ApiOperation(value = "查询平台端仓库", notes = "查询")
    @GetMapping("/getPlatformWarehouseAll")
    public JsonResult getPlatformWarehouseAll(){
        List<Warehouse> list = inventoryOutPlanService.getPlatformWarehouseAll();
        return JsonResult.success(list);
    }
    @ApiOperation(value = "查询是否为云仓供应商", notes = "搜索")
    @GetMapping("/getCloudProvider")
    public JsonResult cloudProvider() {
        return platformClient.cloudProvider("A99999");
    }

}
